<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew ref="searchuinew" :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording"></searchuinew>
      </div>
      <div class="panel-container no-margin">
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="序号" type="index" width="100"></el-table-column>
          <el-table-column align="center" header-align="center" label="信息标题" prop="title" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="地区" prop="area"></el-table-column>
          <el-table-column align="center" header-align="center" label="项目类型" prop="label2"></el-table-column>
          <el-table-column align="center" header-align="center" label="信息类型" prop="hy"></el-table-column>
          <el-table-column align="center" header-align="center" label="渠道" prop="source">
            <template slot-scope="scope"> {{ getSourceName(scope.row.source) }} </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" label="拍卖时间" prop="pm_date"> </el-table-column>
          <el-table-column align="center" header-align="center" label="发布时间" prop="created_time"> </el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" :key="scope.row.id" v-if="scope.row.source !== '富宝'" type="text" class="grey" @click="handleDetail(scope)">信息详情</el-button>
              <el-button size="mini" :key="scope.row.id" v-else disabled type="text" class="grey">信息详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiPMList } from '@/api/industryData'
import { formatFormData } from '@/utils'
import { SourceList } from '@/utils/constant'

export default {
  name: 'index',
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '信息标题：',
          type: 'input',
          field: 'title',
          placeholder: '请输入信息标题',
        },
        {
          fieldName: '地区：',
          type: 'input',
          field: 'prov',
          placeholder: '请输入地区',
        },
        {
          fieldName: '项目类型：',
          type: 'select',
          field: 'label2',
          option: [
            { id: '招标', text: '招标' },
            { id: '拍卖', text: '拍卖' },
            { id: '挂牌', text: '挂牌' },
          ],
          multiple: true,
          placeholder: '请选择渠道',
        },
        {
          fieldName: '信息类型：',
          type: 'input',
          field: 'hy',
          placeholder: '请输入类型',
        },
        {
          fieldName: '渠道：',
          type: 'select',
          field: 'source',
          option: [
            { id: '回收商', text: 'hss' },
            { id: '富宝', text: 'fb' },
            { id: '网优', text: 'wy' },
          ],
          placeholder: '请选择渠道',
        },
        {
          fieldName: '发布时间',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择发布时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],
      isAccording: true,
      searchParams: {
        title: '',
        prov: '',
        label2: [],
        hy: '',
        source: '',
        fb_start_time: '',
        fb_end_time: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.query.source) {
        const { label2, address, start_time, end_time, source, hy } = this.$route.query
        if (label2) {
          this.$refs.searchuinew.clearFormItem('label2', label2.split(','))
        }
        if (address) {
          this.$refs.searchuinew.clearFormItem('prov', address)
        }
        if (start_time) {
          this.$refs.searchuinew.clearFormItem('time', [start_time, end_time])
        }
        if (source) {
          this.$refs.searchuinew.clearFormItem('source', source)
        }
        if (hy) {
          this.$refs.searchuinew.clearFormItem('hy', hy)
        }
      } else {
        this.getDataList()
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const formData = formatFormData(Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams))

      apiPMList(formData)
        .then((res) => {
          const { items, total } = res.result
          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },

    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (item.type === 'timePicker') {
          return
        }
        if (e[item.field] || e[item.field] === 0) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.time) {
        this.searchParams.fb_start_time = e.time[0]
        this.searchParams.fb_end_time = e.time[1]
      } else {
        this.searchParams.fb_start_time = this.searchParams.fb_end_time = ''
      }
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 详情
    handleDetail(scope) {
      if (scope.row.href) {
        window.open(scope.row.href)
      }
    },
    // 获取来源别名
    getSourceName(name) {
      const item = SourceList.find((item) => item.value === name)
      if (item) {
        return item.label
      } else {
        return name
      }
    },
  },
}
</script>

<style scoped lang="scss"></style>
